﻿using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using STF.Blog.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class BlogCateGoryService : IBlogCateGoryService
    {

        private IBlogCateGoryManager _ibl;
        private ICategoryManager _categoryManager;
        public BlogCateGoryService(IBlogCateGoryManager ibl, ICategoryManager categoryManager)
        {
            _ibl = ibl;
            _categoryManager = categoryManager;
        }

        public async Task<int> AddBlogCateGoryAsync(int bid, string cid)
        {
            return await _ibl.AddAsync(new Model.BlogCateGory 
            {
                Bid = bid,
                Cid = cid
            });
        }

        public async Task<int> DeleteBlogCateGoryAsync(int id)
        {
            var data = await _ibl.GetById(id);
            if (data == null)
                return -1;
            if (data.DeleteId != 1)
                return -2;
            return await _ibl.DeleteAsync(data);
        }

        public async Task<List<BlogCateGoryDto>> GetAllBlogCateGoryAsync(int deleteId = 1)
        {  
            var data = await _ibl.GetChooseAsync(bg => bg.DeleteId == deleteId);
            return data == null ? new List<BlogCateGoryDto>() : data.Select(bg => new BlogCateGoryDto
            {
                Id = bg.Id,
                Bid = bg.Bid,
                Cid = bg.Cid,
            }).ToList();
        }

        public async Task<BlogCateGoryDto> GetByIdAsync(int id)
        {
            var data = await _ibl.GetById(id);
            return data == null ? new BlogCateGoryDto() : new BlogCateGoryDto 
            {
                Id = data.Id,
                Bid = data.Bid,
                Cid = data.Cid,
            };
        }

        public async Task<List<BlogCateGoryDto>> GetParentIdOne(int id, int deleteId = 1)
        {
            var data = await _ibl.GetChooseAsync(bd => bd.DeleteId == deleteId);
            var OneDataList = new List<BlogCateGory>();
            foreach (var item in data) 
            {
                var list = item.Cid.IndexOf(",") != -1
                         ? await _categoryManager.GetById(int.Parse(item.Cid.Substring(0, item.Cid.IndexOf(","))))
                         : await _categoryManager.GetById(int.Parse(item.Cid.Substring(0)));
                if (list.ParentId == id)
                    OneDataList.Add(item);
            }

            return OneDataList == null ? new List<BlogCateGoryDto>() : OneDataList.Select(bc => new BlogCateGoryDto
            {
                Id = bc.Id,
                Bid = bc.Bid,
                Cid = bc.Cid
            }).ToList();
        }

        public async Task<List<BlogCateGoryDto>> GetParentIdTwo(int cid, int deleteId = 1)
        {
            var data = await _ibl.GetChooseAsync(bc => bc.DeleteId == deleteId);
            var TwoDataList = new List<BlogCateGory>();
            foreach (var item in data)
            {
                var stringValue = item.Cid;

                for (; ; )
                {
                    if (stringValue.IndexOf(",") == -1) 
                    {
                        if (stringValue.Equals(cid.ToString())) 
                            TwoDataList.Add(item);
                        break;
                    }

                    var nextValue = stringValue.Substring(0,stringValue.IndexOf(","));
                    if (nextValue.Equals(cid.ToString()))
                        TwoDataList.Add(item);

                    stringValue = stringValue.Substring(stringValue.IndexOf(",") + 1);

                }
            }

            return TwoDataList == null ? new List<BlogCateGoryDto>() : TwoDataList.Select(bc => new BlogCateGoryDto 
            {
                Id = bc.Id,
                Bid = bc.Bid,
                Cid = bc.Cid
            }).ToList();
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _ibl.GetById(id);
            if (data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _ibl.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _ibl.GetById(id);
            if (data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _ibl.UpdateAsync(data);
        }

        public async Task<int> UpdateBlogCateGoryAsync(int id, int bid, string cid)
        {
            var data = await _ibl.GetById(id);
            if (data == null)
                return 0;
            data.Bid = bid;
            data.Cid = cid;
            return await _ibl.UpdateAsync(data);
        }
    }
}
